import QtQuick 2.2

Rectangle {
    color: "#ffffff"
    anchors.horizontalCenter: parent.horizontalCenter
    anchors.verticalCenter: parent.verticalCenter
    property alias text: dialog_text.text
    visible: false
    onVisibleChanged: {
        if (visible == true) {
            // largura do texto + 2x o espaçamento padrão do conteúdo para as margens laterais
            width = dialog_text.width + (dialog_content.spacing * 2);
            // altura do texto + 3x o espaçamento padrão do conteúdo para a margem superior, inferior e distância do botão + altura padrão do botão
            height = dialog_text.height + (dialog_content.spacing * 3) + 50;
        } else {
            for(var i = dialog_button_row.children.length; i > 0 ; i--) {
                dialog_button_row.children[i-1].destroy();
            }
        }
    }

    function show(message, buttons, severity) {
        visible = false;
        dialog_text.text = message;

        switch (severity) {
        case 1:
            dialog_text.color = 'black';
            dialog_text.font.bold = true;
            break;
        case 2:
            dialog_text.color = 'red';
            dialog_text.font.bold = true;
            break;
        default:
            dialog_text.color = 'black';
            dialog_text.font.bold = false;
        }

        if (buttons !== undefined) {
            var component = Qt.createComponent("DialogButton.qml");
            for(var i = 0; i < buttons.length; i++) {
                var text = buttons[i]["text"];
                var width = buttons[i]["width"];
                var action = buttons[i]["action"];
                var button = component.createObject(dialog_button_row, {"text": text, "width": width, "height": 50, "parent_dialog": this});
                button.buttonSelected.connect(action);
            }
        }

        visible = true;
    }

    function hide() {
        visible = false;
    }

    Column {
        id: dialog_content
        spacing: 20
        anchors.fill: parent
        anchors.topMargin: 15

        Text {
            id: dialog_text
            color: 'red'
            font.bold: true
            font.pixelSize: 28
            anchors.horizontalCenter: parent.horizontalCenter
        }

        Row {
            id: dialog_button_row
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: 20
        }
    }
}
